/**
 * 初始化cleanKeeping详情对话框
 */
var StoreyInfoDlg = {
    storeyInfoData: {},
    validateFields: {
        name: {
            validators: {
                notEmpty: {
                    message: '办公楼名称不能为空'
                }
            }
        },
        pareaTypes: {
            validators: {
                notEmpty: {
                    message: '巡视区域类型不能为空'
                }
            }
        }
    }
};

/**
 * 清除数据
 */
StoreyInfoDlg.clearData = function () {
    this.storeyInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
StoreyInfoDlg.set = function (key, val) {
    this.storeyInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
StoreyInfoDlg.get = function (key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
StoreyInfoDlg.close = function () {
    parent.layer.close(window.parent.Storey.layerIndex);
}

/**
 * 收集数据
 */
StoreyInfoDlg.collectData = function () {
    this
        .set('id')
        .set('name')
        .set("pareaTypes");
}
StoreyInfoDlg.getCheckboxValaus = function () {
    var pareaTypes = "";
    $("input:checkbox[name='pareaTypes']:checked").each(function () { // 遍历name=test的多选框
        pareaTypes +=$(this).val()+",";  // 每一个被选中项的值
    });
    $("#pareaTypes").val(pareaTypes);
}
/**
 * 验证数据是否为空
 */
StoreyInfoDlg.validate = function () {
    $('#storeyInfoForm').data("bootstrapValidator").resetForm();
    $('#storeyInfoForm').bootstrapValidator('validate');
    return $("#storeyInfoForm").data('bootstrapValidator').isValid();
};
/**
 * 提交添加
 */
StoreyInfoDlg.addSubmit = function () {
    this.getCheckboxValaus();
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/storey/add", function (data) {
        Feng.success("添加成功!");
        window.parent.Storey.table.refresh();
        StoreyInfoDlg.close();
    }, function (data) {
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.storeyInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
StoreyInfoDlg.editSubmit = function () {
    this.getCheckboxValaus();
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/storey/update", function (data) {
        Feng.success("修改成功!");
        window.parent.Storey.table.refresh();
        StoreyInfoDlg.close();
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.storeyInfoData);
    ajax.start();
}

$(function () {
    Feng.initValidator("storeyInfoForm", StoreyInfoDlg.validateFields);
});
